package com.wish233.wiliwilivideo.controller;

import com.wish233.wiliwilicommon.common.BaseResponse;
import com.wish233.wiliwilicommon.common.ErrorCode;
import com.wish233.wiliwilicommon.common.ResultUtils;
import com.wish233.wiliwilicommon.exception.BusinessException;
import com.wish233.wiliwilicommon.exception.ThrowUtils;
import com.wish233.wiliwilivideo.domain.dto.comment.CreateCommentRequest;
import com.wish233.wiliwilivideo.domain.dto.comment.UpdateCommentRequest;
import com.wish233.wiliwilivideo.domain.vo.CommentVO;
import com.wish233.wiliwilivideo.service.CommentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author WHH
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @ApiOperation("通过ID查找评论")
    @GetMapping("/{id}")
    public BaseResponse<CommentVO> getCommentById(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null, new BusinessException(ErrorCode.PARAMS_ERROR));
        return ResultUtils.success(commentService.getCommentById(id));
    }

    @PostMapping()
    @ApiOperation("创建评论")
    public BaseResponse<Long> createComment(@RequestBody CreateCommentRequest createCommentRequest) {
        ThrowUtils.throwIf(createCommentRequest == null, new BusinessException(ErrorCode.PARAMS_ERROR));
        return ResultUtils.success(commentService.createComment(createCommentRequest));
    }

    @ApiOperation("更新评论")
    @PutMapping()
    public BaseResponse<Boolean> updateComment(@RequestBody UpdateCommentRequest updateCommentRequest) {
        ThrowUtils.throwIf(updateCommentRequest == null, new BusinessException(ErrorCode.PARAMS_ERROR));
        return ResultUtils.success(commentService.updateComment(updateCommentRequest));
    }

    @ApiOperation("删除")
    @PutMapping("/{id}")
    public BaseResponse<Boolean> deleteComment(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null, new BusinessException(ErrorCode.PARAMS_ERROR));
        return ResultUtils.success(commentService.deleteComment(id));
    }

    @ApiOperation("展示一个视频的根评论(分页）")
    @GetMapping("/showVideoRootComment/{id}")
    public BaseResponse<List<CommentVO>> showVideoRootComment(@PathVariable Long id,
                                                              @RequestParam int current,
                                                              @RequestParam int pageSize) {
        ThrowUtils.throwIf(id == null, new BusinessException(ErrorCode.PARAMS_ERROR,"id不能为空"));
        ThrowUtils.throwIf(current < 1 || pageSize < 1, new BusinessException(ErrorCode.PARAMS_ERROR,"current或pageSize错误"));
        return ResultUtils.success(commentService.showVideoRootComment(id, current, pageSize));
    }
    @ApiOperation("展示一个根评论的子评论(分页）")
    @GetMapping("/showChildComment/{id}")
    public BaseResponse<List<CommentVO>> showChildCommentById(@PathVariable Long id,
                                                              @RequestParam int current,
                                                              @RequestParam int pageSize) {
        ThrowUtils.throwIf(id == null, new BusinessException(ErrorCode.PARAMS_ERROR,"id不能为空"));
        ThrowUtils.throwIf(current < 1 || pageSize < 1, new BusinessException(ErrorCode.PARAMS_ERROR,"current或pageSize错误"));
        return ResultUtils.success(commentService.showChildCommentById(id, current, pageSize));
    }
}
